package com.njitzx.service.impl;

import com.njitzx.controller.GoodsPicsController;
import com.njitzx.entity.dto.GoodsDTO;
import com.njitzx.entity.dto.GoodsPageDTO;
import com.njitzx.entity.po.Attribute;
import com.njitzx.entity.po.Goods;
import com.njitzx.entity.po.GoodsAttr;
import com.njitzx.entity.po.GoodsPics;
import com.njitzx.entity.vo.AttrVO;
import com.njitzx.entity.vo.GoodsPageVO;
import com.njitzx.entity.vo.GoodsVO;
import com.njitzx.mapper.AttributeMapper;
import com.njitzx.mapper.GoodsAttrMapper;
import com.njitzx.mapper.GoodsMapper;
import com.njitzx.mapper.GoodsPicsMapper;
import com.njitzx.service.GoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author 周鑫
 * @since 2024-08-20
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class GoodsServiceImpl implements GoodsService {

    private final GoodsMapper goodsMapper;
    private final GoodsPicsMapper goodsPicsMapper;
    private final AttributeMapper attributeMapper;
    private final GoodsAttrMapper goodsAttrMapper;

    /**
     * 商品表分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, GoodsPageDTO goodsPageDTO) {
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsPageVO> goodsList = goodsMapper.selectByPage(goodsPageDTO);
        PageInfo<GoodsPageVO> pageInfo = PageInfo.of(goodsList);
        Map<String, Object> map = new HashMap<>();
        map.put("goods", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        map.put("pageNum", pageInfo.getPageNum());
        return map;
    }

    /**
     * 根据主键查询商品表
     *
     * @param goodsId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public GoodsVO findById(Integer goodsId) {

        Goods goods = goodsMapper.selectById(goodsId);
        GoodsVO goodsVO = new GoodsVO();
        BeanUtils.copyProperties(goods, goodsVO); //拷贝属性
        //分类id
        String goodsCat = goods.getCatOneId() + "," + goods.getCatTwoId() + "," + goods.getCatThreeId();
        goodsVO.setGoodsCat(goodsCat);


        GoodsPics goodsPics1 = new GoodsPics();
        goodsPics1.setGoodsId(goodsId);
        List<GoodsPics> goodsPics = goodsPicsMapper.select(goodsPics1);
        goodsVO.setPics(goodsPics);

        GoodsAttr attr = new GoodsAttr();
        attr.setGoodsId(goodsId);
        List<AttrVO> attrVO = attributeMapper.selectattVO(attr);
        goodsVO.setAttrs(attrVO);
        return goodsVO;
    }


    /**
     * 商品表新增
     *
     * @param
     */
    public void add(GoodsDTO goodsDTO) {
        goodsDTO.setCreateTime(new Date());
        goodsDTO.setUpdateTime(new Date());
        //分类一定是 含有的
        String[] split = goodsDTO.getGoodsCat().split(",");
        goodsDTO.setCatOneId(Integer.parseInt(split[0]));
        goodsDTO.setCatTwoId(Integer.parseInt(split[1]));
        goodsDTO.setCatThreeId(Integer.parseInt(split[2]));
        goodsDTO.setCatId(Integer.parseInt(split[2]));
        goodsMapper.insert(goodsDTO);
        //拿到分类参数
        List<GoodsAttr> attrs = goodsDTO.getAttrs();
        System.out.println(attrs);

        //编辑商品的属性
        if (attrs != null) {
            goodsAttrMapper.insertBatch(goodsDTO.getGoodsId(), attrs);
        }

        List<GoodsPics> goodsPics = goodsDTO.getPics();
        goodsPicsMapper.insertBatch(goodsDTO.getGoodsId(), goodsPics);

    }

    /**
     * 修改商品表
     *
     * @param
     */
    @Override
    public void modify(GoodsDTO goodsDTO) {
        Goods goods = new Goods();
        String[] split = goodsDTO.getGoodsCat().split(",");
        goodsDTO.setCatOneId(Integer.parseInt(split[0]));
        goodsDTO.setCatTwoId(Integer.parseInt(split[1]));
        goodsDTO.setCatThreeId(Integer.parseInt(split[2]));
        goodsDTO.setCatId(Integer.parseInt(split[2]));
        goodsDTO.setUpdateTime(new Date());

        BeanUtils.copyProperties(goodsDTO, goods);

        goodsMapper.update(goods);

        //修改图片和属性
        List<GoodsAttr> attrs = goodsDTO.getAttrs();

        goodsAttrMapper.deleteByGoodsId(goodsDTO.getGoodsId());
        //编辑商品的属性
        if (attrs.size()>0) {
            goodsAttrMapper.insertBatch(goodsDTO.getGoodsId(), attrs);
        }

        List<GoodsPics> goodsPics = goodsDTO.getPics();
        //新增加的
        GoodsPics goodsPics1 = new GoodsPics();
        goodsPics1.setGoodsId(goodsDTO.getGoodsId());
        //现在含有的id
        List<Integer> collect = goodsPicsMapper.select(goodsPics1).stream().map(GoodsPics::getPicsId).collect(Collectors.toList());
        List<Integer> ge = goodsPics.stream().filter(item -> item.getPicsId() != null).map(GoodsPics::getPicsId).collect(Collectors.toList());
        collect.forEach(item -> {
            if (!ge.contains(item)) {
                goodsPicsMapper.deleteById(item);
            }
        });
        goodsPics.forEach(item -> {
            if (item.getPicsId() == null) {
                item.setGoodsId(goodsDTO.getGoodsId());
                goodsPicsMapper.insert(item);
            }
        });
    }

    /**
     * 根据主键删除商品表
     *
     * @param goodsId
     */
    @Override
    public void removeById(Integer goodsId) {
            goodsMapper.deleteById(goodsId);

            goodsPicsMapper.deleteByGoodsId(goodsId);

            goodsAttrMapper.deleteByGoodsId(goodsId);
    }

    @Override
    public void updateStatus(Integer goodsId, Integer goodsState) {
         goodsMapper.updateStatus(goodsId,goodsState);
    }
}
